/**
 * file name  : Jenoware/examples/JenoHWE.java
 * authors    : Xumerle Luciano
 * created    : mer 14 jun  2006 10:05:00 CET
 *
 * Copyright (c) 2004-2007 Luciano Xumerle. All rights reserved.
 *
 * This file is part of gephinfo.
 *
 * gephinfo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * COMPILE:
 * javac -classpath jenoware.jar JenoHWE
 *
 * EXECUTE:
 * java -classpath jenoware.jar:. JenoHWE [options]
 *
 * HELP:
 * java -classpath jenoware.jar:. JenoHWE -h
 *
 * The program tests for Hardy-Weinberg equilibrium.
 *
 */

// import jenoware packages
import it.ciano.jenoware.Pedigree;
import it.ciano.jenoware.toPedigree;
import it.ciano.jenoware.Sbj;
import it.ciano.jenoware.MarkerFreq;
import it.ciano.jenoware.HWE;
import it.ciano.util.ReadPar;
import it.ciano.util.CString;

public class JenoHWE {
    public JenoHWE() {
    }

    public static void main ( String[] args )
    throws java.io.IOException {
        ReadPar par = myPar ( args );
        par.doMsg();
        int simulations = 0;
        if ( !par.checkPar() || par.getParIndex ( "h" ) > -1 )
            par.doHelp( );
        else
        {
            // manage separator
            String separator = ";";
            if (  par.getParIndex ( "t" ) > -1 )
                separator="\t";
            // retrive number of simulations from command line
            simulations = getInt ( par.getParValue ( "nsim" ) );

            // parse CSV file
            if ( par.getParIndex ( "csv" ) > -1 ) {
                // the Pedigree object
                Pedigree ped = new Pedigree();
                // add all the CSV files to the Pedigree
                toPedigree.importCSV ( ped, par.getParValue ( "csv" ), separator );
                // the list of Sbj
                Sbj[] ids = ped.getListSbj();
                // print markers statistics
                String[] markers = ped.getMarkersList();
                if ( markers != null ) {
                    System.out.println ( "Printing Genotype Statistics:\n" );
                    for ( int i = 0; i < markers.length; i++ ) {
                        System.out.println ( ped.pedCheck ( markers[ i ] ) );
                        MarkerFreq ll = new MarkerFreq ( ped, markers[ i ] );
                        // print the descriptive statistics
                        System.out.println ( ll );
                        // print test for Hardy-Weinberg equilibrium
                        System.out.println ( ll.getHWE ( simulations, true ) );
                    }
                }
            }

            // read genotype freq from command line
            if ( par.getParIndex ( "d" ) > -1 ) {
                System.out.println ( "Input required for option -d:" );
                System.out.println ( "  Number of alleles." );
                System.out.println ( "  Number of observed genotypes with order:" );
                System.out.println ( "    1/1  1/2  2/2  1/3  2/3  3/3  1/4  2/4 etc.\n" );
                int alleleNum = getInt ( par.getOptionalAdditionalPar ( 1 ) );
                if ( alleleNum > 0 ) {
                    int genoNum = HWE.getGenotypeClassesFromAlleleClasses ( alleleNum );
                    int geno[] = new int[ genoNum ];
                    for ( int i = 0; i < genoNum; i++ ) {
                        int fr = getInt ( par.getOptionalAdditionalPar ( 2 + i ) );
                        if ( fr > -1 )
                            geno[ i ] = fr;
                        else
                            return ;
                    }
                    System.out.println ( new HWE ( geno, simulations, true ).toString() );
                }
            }
        }
    } // end main

    private static int getInt ( String s ) {
        Integer ii = CString.getInteger ( s );
        if ( ii == null )
            return -1;
        return ii.intValue();
    }

    private static ReadPar myPar ( String[] input ) {
        ReadPar parameter = new ReadPar ( 100, "2005" );
        parameter.addSintaxHelp ( "JenoHWE [options]" );
        parameter.addMessageInfo ( "JenoHWE", "0.0.3", "6 apr 2005", "2005",
                                   "Luciano Xumerle <luciano.xumerle@medgen.univr.it>" );
        parameter.addPar ( "h", "Print this help", false );
        parameter.addPar ( "d", "Number of allele and genotype Freq.", false );
        parameter.addWhiteHelpLine ( "d" );
        parameter.addPar ( "nsim", "Number of simulations [default 0]", true );
        parameter.addPar ( "csv", "Calculates geno freq from a CSV file.", true );
        parameter.addPar ( "t", "Separator is <TAB> (default is ';')", false );
        parameter.parsePar ( input );
        return parameter;
    }

} //end class

